package com.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import backpage.model.DBAccessor;
import backpage.view.PageDivision;
import backpage.view.ResItem;

public class onResPageTurned extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 979090501280944163L;

	/**
	 * Constructor of the object.
	 */
	public onResPageTurned() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {System.out.println("redirected");
		String resType = request.getParameter("resType");
		String sortKey = request.getParameter("sortKey");
		String upOrDown;
		String pageCount = request.getParameter("numInOnePage");
		String curPage = request.getParameter("curPage");
		
		if( resType == null )
			resType = "doc";
		if( sortKey == null || sortKey == ""){
			sortKey = "file_name";
			upOrDown = "asc";
		}else{
			if( sortKey.charAt(0) == '1' )
				upOrDown = "desc";
			else upOrDown = "asc";
			if( sortKey.substring(1).equals("fileName") )
				sortKey = "file_name";
			else if( sortKey.substring(1).equals("createTime") )
				sortKey = "create_time";
			else if( sortKey.substring(1).equals("downloadNum") )
				sortKey = "download_num";
			else if( sortKey.substring(1).equals("touchNum") )
				sortKey = "touch_num";
		}
		if( pageCount == null )
			pageCount = "8";
		if( curPage == null )
			curPage = "1";
		
		PageDivision pageDivision =
			DBAccessor.Instance.getPageDivision(resType, sortKey, upOrDown , pageCount, curPage);
		
		request.setAttribute("pageDivision", pageDivision);
		
		//increase the touch_num, if needed
		ArrayList<String> touchNumList = null;
		if( request.getSession().getAttribute("touchNumList") != null)
			touchNumList = (ArrayList<String>)request.getSession().getAttribute("touchNumList");
		else touchNumList = new ArrayList<String>();
		for( int i=0; i<pageDivision.getitemList().size(); i++ ){	//for each requested ResItem
			int id = ( (ResItem)pageDivision.getitemList().get(i) ).getID();
			if( !touchNumList.contains( Integer.toString(id) ) ){	//not requested before
				touchNumList.add(Integer.toString(id));
				DBAccessor.Instance.incTouchNum(Integer.toString(id));
			}
		}
		request.getSession().setAttribute("touchNumList", touchNumList);		//write the touched IDs back to the session
		
		RequestDispatcher rDispatcher;
		if( request.getParameter("resType") == null )
			rDispatcher = request.getRequestDispatcher("/index.jsp?resType=doc");
		else rDispatcher = request.getRequestDispatcher("/index.jsp");
		if( rDispatcher != null ) rDispatcher.forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
